<?php
//$Id: new2.php 47 2006-01-19 02:55:27Z phpfixer $
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $user_info;
global $account_creation_closed, $dbtables;

// Check if user already has a BNT account
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);

// If the user already has an account, run the pre-login checks
if ($smcFunc['db_num_rows']($result) <> 0)
{
	$smcFunc['db_free_result']($result);
	TextMainMenu();
	redirectexit('action=blacknova;sa=main');
}
else
{
	bigtitle($txt['bnt_new_title2']);

	if ($account_creation_closed)
	{
		echo '
						<p>' . $txt['bnt_new_closed_message'] . '</p>';
		return;
	}

	$character_name = htmlspecialchars($_POST['character_name']);
	$ship_name = htmlspecialchars($_POST['ship_name']);
	$character_name = ereg_replace("[^[:digit:][:space:][:alpha:][\']]"," ",$character_name);
	$ship_name = ereg_replace("[^[:digit:][:space:][:alpha:][\']]"," ",$ship_name);

	if (!get_magic_quotes_gpc())
	{
		$character_name = addslashes($character_name);
		$ship_name = addslashes($ship_name);
	}

	$result = $smcFunc['db_query']('', '
		SELECT character_name, ship_name, user_id
		FROM {raw:db_ships}
		WHERE character_name = {string:character_name}
		OR ship_name = {string:ship_name}',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
			'character_name' => $character_name,
			'ship_name' => $ship_name
		)
	);

	$flag = 0;

	// Check for blank fields
	if ($character_name == '' || $ship_name == '' )
	{ 
		echo '
						<p>', $txt['bnt_new_blank'], '</p>';
		$flag = 1;
	}

	// Check for duplicate names
	if (($smcFunc['db_num_rows']($result) > 0) && ($flag == 0))
	{
		while ($row = $smcFunc['db_fetch_assoc']($result))
		{
			if (strtolower($row['character_name']) == strtolower($character_name))
			{
				$txt['bnt_new_inusechar'] = str_replace("[character]", $row['character_name'], $txt['bnt_new_inusechar']);
				echo '
						<p>', $txt['bnt_new_inusechar'], '</p>';
				$flag = 1;
			}
			if (strtolower($row['ship_name']) == strtolower($ship_name))
			{
				$txt['bnt_new_inuseship'] = str_replace("[ship]", $row['ship_name'], $txt['bnt_new_inuseship']);
				echo '
						<p>', $txt['bnt_new_inuseship'], '</p>';
				$flag = 1;
			}
			if ($row['user_id'] == $user_info['user_id'])
			{
				echo '
						<p>', $txt['bnt_new_inuseid'], '</p>';
				$flag = 1;
			}
		}
	}

	$smcFunc['db_free_result']($result);

	// If the new player has cleared all checks, create the player
	if ($flag == 0)
	{
		global $max_turns;
		global $start_armor, $start_credits, $start_energy, $start_fighters;
		global $start_editors, $start_genesis, $start_beacon, $start_emerwarp;
		global $start_minedeflectors, $start_lssd, $start_escape, $start_scoop;
		global $default_prod_ore, $default_prod_organics, $default_prod_goods;
		global $default_prod_energy, $default_prod_fighters, $default_prod_torp;
		
		// Insert the new player into the database
		$smcFunc['db_insert']('insert',
			$dbtables['ships'],
			array(
				'ship_name' => 'string',
				'user_id' => 'int',
				'ship_destroyed' => 'string',
				'character_name' => 'string',
				'armor_pts' => 'int',
				'credits' => 'int',
				'ship_energy' => 'int',
				'ship_fighters' => 'int',
				'turns' => 'int',
				'on_planet' => 'string',
				'dev_warpedit' => 'int',
				'dev_genesis' => 'int',
				'dev_beacon' => 'int',
				'dev_emerwarp' => 'int',
				'dev_escapepod' => 'string',
				'dev_fuelscoop' => 'string',
				'dev_minedeflector' => 'int',
				'last_login' => 'string',
				'ip_address' => 'string',
				'trade_colonists' => 'string',
				'trade_fighters' => 'string',
				'trade_torps' => 'string',
				'trade_energy' => 'string',
				'dev_lssd' => 'string',
				'global_ore' => 'int',
				'global_organics' => 'int',
				'global_goods' => 'int',
				'global_energy' => 'int',
				'global_fighters' => 'int',
				'global_torps' => 'int',
			),
			array(
				$ship_name,
				$user_info['id'],
				'N',
				$character_name,
				$start_armor,
				$start_credits,
				$start_energy,
				$start_fighters,
				$max_turns,
				'N',
				$start_editors,
				$start_genesis,
				$start_beacon,
				$start_emerwarp,
				$start_escape,
				$start_scoop,
				$start_minedeflectors,
				Now(),
				$_SERVER['REMOTE_ADDR'],
				'Y',
				'N',
				'N',
				'Y',
				$start_lssd,
				$default_prod_ore,
				$default_prod_organics,
				$default_prod_goods,
				$default_prod_energy,
				$default_prod_fighters,
				$default_prod_torp,
			),
			''
		);

		$result = $smcFunc['db_query']('', '
			SELECT ship_id
			FROM {raw:db_ships}
			WHERE user_id = {int:user_id}
			LIMIT 1',
			array(
				'db_ships' => $dbtables['ships'],
				'user_id' => $user_info['id'],
			)
		);
		$shipid = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
		
		// Create a new zone for the player
		$smcFunc['db_insert']('insert',
			$dbtables['zones'],
			array(
				'zone_name' => 'string',
				'owner' => 'int',
				'corp_zone' => 'string',
				'allow_beacon' => 'string',
				'allow_attack' => 'string',
				'allow_planetattack' => 'string',
				'allow_warpedit' => 'string',
				'allow_planet' => 'string',
				'allow_trade' => 'string',
				'allow_defenses' => 'string',
				'max_hull' => 'int',
			),
			array(
				$character_name . '\'s Territory',
				$shipid['ship_id'],
				'N',
				'Y',
				'Y',
				'Y',
				'Y',
				'Y',
				'Y',
				'Y',
				0,
			),
			''
		);
		
		/*	Update the first available empty sector closest to federation space.
			This will be the new player new home zone.
		*/
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_universe}
			SET zone_id = {int:player_zone}
			WHERE zone_id = 1
			ORDER BY sector_id ASC
			LIMIT 1',
			array(
				'db_universe' => $dbtables['universe'],
				'player_zone' => $smcFunc['db_insert_id']($dbtables['zones'], 'zone_id')
			)
		);
		
		// Create an IGB bank account for the player.
		$smcFunc['db_insert']('insert',
			$dbtables['ibank_accounts'],
			array(
				'ship_id' => 'string',
				'balance' => 'int',
				'loan' => 'string',
			),
			array(
				$shipid['ship_id'],
				0,
				0
			),
			''
		);
		
		TextMainMenu();
		redirectexit('action=blacknova;sa=main');
	}
	else
	{
		$txt['bnt_new_err'] = str_replace("[scripturl]", $scripturl, $txt['bnt_new_err']);
		echo '
						<p>', $txt['bnt_new_err'], '</p>';
	}
}
?>
